﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;


namespace DomainModel
{
    public class Job : EntityBase
    {
        public string JobGUID { get; set; }
        public int JobID { get; set; }
        public int CoID { get; set; }
        public int TotalCount { get; set; }
        //0 = new
        //1 = active
        //2 = expired
        public int Status { get; set; }

        [Required]
        [NoHtml]
        [Display(Name = "Job Title")]
        [StringLength(64, ErrorMessage = "The Job Title field has maximum 64 characters")]
        public string Title { get; set; }

        [Required]
        [Display(Name = "Job Description")]
        [StringLength(8000, ErrorMessage = "The Job Description field has maximum 8000 characters")]
        public string Description { get; set; }

        public int JobTypeID { get; set; }
        public int JobExpID { get; set; }

        [Required]
        [NoHtml]
        [Display(Name = "Location")]
        [StringLength(64, ErrorMessage = "The Location field has maximum 64 characters")]
        public string Location { get; set; }
        public Boolean AllowTelecommute { get; set; }
        public Boolean RelocationOffered { get; set; }

        [NoHtml]
        [Display(Name = "Compensation")]
        [StringLength(64, ErrorMessage = "The Compensation field has maximum 64 characters")]
        public string Compensation { get; set; }

        [NoHtml]
        [Display(Name = "Job Perks")]
        [StringLength(500, ErrorMessage = "The Job Perks field has maximum 500 characters")]
        public string Perk { get; set; }

        public Boolean ApplyViaEmail { get; set; } //if Yes, use ApplyEmail, if no, use Apply url means, apply via url

        //[Required]
        [Email]
        [Display(Name = "Email")]
        [StringLength(64, ErrorMessage = "The Email field has maximum 64 characters")]
        public string ApplyEmail { get; set; }

        //[Required]
        [Url]
        [Display(Name = "Url")]
        [StringLength(255, ErrorMessage = "The Url field has maximum 255 characters")]
        public string ApplyUrl { get; set; }

        public Boolean OnlinePortfolioRequired { get; set; }
        public Boolean OnlinePDFResumeRequired { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime PublishDate { get; set; }

        public DateTime SavedDate { get; set; }

        //extra property 
        [NoHtml]
        [StringArrayMaxLength(Delimeter=",", MaximumCount=5, MaximumLengthForInnerEach=32,
            ErrorMessage="Please no more than 5 keywords", 
            ErrorMessageForInnerEach="Each keyword has maximum 32 characters")]
        public string JobKeywords { get; set; }

        //all calc fields
        public string JobExp { get; set; }
        public string JobType { get; set; }
        public string JobToken
        {
            get
            {
                return JobGUID + "-" + JobID.ToString();
            }
        }

        public List<string> JobKeywordList
        {
            get {
                if (string.IsNullOrEmpty(JobKeywords))
                {
                    return new List<string>();
                }
                else
                {
                    string[] results = JobKeywords.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    return results.Select(x => x.Trim().ToLower()).ToList();
                }
            }
        }

        public string ListingStatus
        {
            get
            {
                if (Status == 0)
                {
                    return "Unpublished";
                }
                else if (Status == 1)
                {
                    return "Active";
                }
                else if (Status == 2)
                {
                    return "Expired";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public Boolean Editable {
            get {
                return Status == 1 && PublishDate != null && PublishDate.AddHours(72) > DateTime.Now;
            }
        }

        public int EditableHours {
            get {
                TimeSpan span = DateTime.Now - PublishDate;
                int hourDiff = 72 - (int)span.TotalHours;
                return (hourDiff <= 0) ? 1 : hourDiff;
            }
        }

        public Job() 
        {
            //default value
            ApplyViaEmail = true;    
        }
    }
}
